/**
 * @author cgull
 */

$(function() {
	$("#replaceMessage").hide();
	
	//clear window when clicked
	$(":input").focus(function (){
		if($(this).val() == "Enter your text in here and click 'Process' below.")
			$(this).html("")
	});
	
});

// http://www.javascriptkit.com/jsref/regexp.shtml
function html2entities(sometext) {
	replaceCount = 0;
	var re = /[¡¢£©®ÀÁÂÃÄÅÇÈÉÊËÌÍÑÒÓÔÕÖÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ–—™“″¿]/g; //BE SURE IF IT EXISTS HERE, IT'S LISTED BELOW!!!!
	var fieldText = $(":input").val(); 
	
	$(":input").val(fieldText.replace(re, function(m) { return replacechar(m) } ));
	
	drawPreview();
	updateCountDisplay(replaceCount);
}

function updateCountDisplay(count){
	$("#replaceMessage").html(count + " Replaced");
	$("#replaceMessage").fadeIn();
	$("#replaceMessage").fadeOut(1500);
}

function replacechar(match) {
	//if (match == "<"){ replaceCount++; return "&lt;"
	//} else if (match == ">"){ replaceCount++; return "&gt;"
	//} else if (match == "\""){ replaceCount++; return "&quot;"
	//} else if (match == "'"){ replaceCount++; return "&#039;"
	//} else if (match == "&"){ replaceCount++; return "&amp;"
	if (match == "™"){ replaceCount++; return "&trade;"
	} else if (match == "“"){ replaceCount++; return '"'
	} else if (match == "″"){ replaceCount++; return '"'
	} else if (match == "¿"){ replaceCount++; return "&iquest;"
	} else if (match == "©"){ replaceCount++; return "&copy;"
	} else if (match == "®"){ replaceCount++; return "<span style='font-size:7px; position:relative; top:-5px;'>&reg;</span>"
	} else if (match == "è"){ replaceCount++; return "&egrave;"
	} else if (match == "¡"){ replaceCount++; return "&iexcl;"
	} else if (match == "¢"){ replaceCount++; return "&cent;"
	} else if (match == "£"){ replaceCount++; return "&pound;"
	} else if (match == "À"){ replaceCount++; return "&Agrave;"
	} else if (match == "Á"){ replaceCount++; return "&Aacute;"
	} else if (match == "Â"){ replaceCount++; return "&Acirc;"
	} else if (match == "Ã"){ replaceCount++; return "&Atilde;"
	} else if (match == "Ä"){ replaceCount++; return "&Auml;"
	} else if (match == "Å"){ replaceCount++; return "&Aring;"
	} else if (match == "Ç"){ replaceCount++; return "&Ccedil;"
	} else if (match == "È"){ replaceCount++; return "&Egrave;"
	} else if (match == "É"){ replaceCount++; return "&Eacute;"
	} else if (match == "Ê"){ replaceCount++; return "&Ecirc;"
	} else if (match == "Ë"){ replaceCount++; return "&Euml;"
	} else if (match == "Ì"){ replaceCount++; return "&Igrave;"
	} else if (match == "Í"){ replaceCount++; return "&Iacute;"
	} else if (match == "Ñ"){ replaceCount++; return "&Ntilde;"
	} else if (match == "Ò"){ replaceCount++; return "&Ograve;"
	} else if (match == "Ó"){ replaceCount++; return "&Oacute;"
	} else if (match == "Ô"){ replaceCount++; return "&Ocirc;"
	} else if (match == "Õ"){ replaceCount++; return "&Otilde;"
	} else if (match == "Ö"){ replaceCount++; return "&Ouml;"
	} else if (match == "Ù"){ replaceCount++; return "&Ugrave;"
	} else if (match == "Ú"){ replaceCount++; return "&Uacute;"
	} else if (match == "Û"){ replaceCount++; return "&Ucirc;"
	} else if (match == "Ü"){ replaceCount++; return "&Uuml;"
	} else if (match == "Ý"){ replaceCount++; return "&Yacute;"
	} else if (match == "Þ"){ replaceCount++; return "&THORN;"
	} else if (match == "ß"){ replaceCount++; return "&szlig;"
	} else if (match == "à"){ replaceCount++; return "&agrave;"
	} else if (match == "á"){ replaceCount++; return "&aacute;"
	} else if (match == "â"){ replaceCount++; return "&acirc;"
	} else if (match == "ã"){ replaceCount++; return "&atilde;"
	} else if (match == "ä"){ replaceCount++; return "&auml;"
	} else if (match == "å"){ replaceCount++; return "&aring;"
	} else if (match == "æ"){ replaceCount++; return "&aelig;"
	} else if (match == "ç"){ replaceCount++; return "&ccedil;"
	} else if (match == "è"){ replaceCount++; return "&egrave;"
	} else if (match == "é"){ replaceCount++; return "&eacute;"
	} else if (match == "ê"){ replaceCount++; return "&ecirc;"
	} else if (match == "ë"){ replaceCount++; return "&euml;"
	} else if (match == "ì"){ replaceCount++; return "&igrave;"
	} else if (match == "í"){ replaceCount++; return "&iacute;"
	} else if (match == "î"){ replaceCount++; return "&icirc;"
	} else if (match == "ï"){ replaceCount++; return "&iuml;"
	} else if (match == "ð"){ replaceCount++; return "&eth;"
	} else if (match == "ñ"){ replaceCount++; return "&ntilde;"
	} else if (match == "ò"){ replaceCount++; return "&ograve;"
	} else if (match == "ó"){ replaceCount++; return "&oacute;"
	} else if (match == "ô"){ replaceCount++; return "&ocirc;"
	} else if (match == "õ"){ replaceCount++; return "&otilde;"
	} else if (match == "ö"){ replaceCount++; return "&ouml;"
	} else if (match == "ø"){ replaceCount++; return "&oslash;"
	} else if (match == "ù"){ replaceCount++; return "&ugrave;"
	} else if (match == "ú"){ replaceCount++; return "&uacute;"
	} else if (match == "û"){ replaceCount++; return "&ucirc;"
	} else if (match == "ü"){ replaceCount++; return "&uuml;"
	} else if (match == "ý"){ replaceCount++; return "&yacute;"
	} else if (match == "þ"){ replaceCount++; return "&thorn;"
	} else if (match == "ÿ"){ replaceCount++; return "&yuml;"
	} else if (match == "–"){ replaceCount++; return "&ndash;"
	} else if (match == "—"){ replaceCount++; return "&mdash;"
	} else if (match == "Œ"){ replaceCount++; return "&#338;"
	} else if (match == "«"){ replaceCount++; return "&laquo;"
	} else if (match == "»"){ replaceCount++; return "&raquo;"
	} else if (match == "œ"){ replaceCount++; return "&#339;"}
}

function drawPreview(){
	$("#right").html($(":input").val());
	
}